<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          style="width: 220px"
          maxlength="10"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <div class="searchReset">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </div>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['videoCamera:videoCamera:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['videoCamera:videoCamera:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <div
      :class="eleTabTop <= 85 ? 'eleTab' : ''"
      class="informationTab"
      ref="eleTab"
    >
      <el-table
        v-loading="loading"
        :data="videoCameraList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="序号"
          align="center"
          prop="id"
          type="index"
          width="100"
        />
        <el-table-column
          label="股道"
          align="center"
          prop="trackName"
          width="150"
        />
        <el-table-column
          label="列位"
          align="center"
          prop="completeName"
          width="250"
        />
        <el-table-column
          label="名称"
          prop="name"
          min-width="200"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="智能灯编号"
          prop="ipaddress"
          min-width="200"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="备注"
          prop="remark"
          min-width="200"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['videoCamera:videoCamera:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              class="delBtn_R"
              @click="handleDelete(scope.row)"
              v-hasPermi="['videoCamera:videoCamera:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改摄像机对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="股道" prop="trackId">
          <el-select v-model="form.trackId" placeholder="请选择股道">
            <el-option
              v-for="item in trackTeamLists"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="列位" prop="complete">
          <el-select v-model="form.complete" placeholder="请选择列位">
            <el-option
              v-for="item in completeLists"
              :key="item.complete"
              :label="item.completeName"
              :value="item.complete"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入名称"
            maxlength="15"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="智能灯编号" prop="ipaddress">
          <el-input
            v-model="form.ipaddress"
            placeholder="请输入智能灯编号"
            maxlength="20"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            placeholder="请输入备注"
            type="textarea"
            maxlength="100"
            show-word-limit
            :rows="10"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  lamplist,
  listVideoCamera,
  getVideoCamera,
  delVideoCamera,
  addVideoCamera,
  updateVideoCamera,
} from "@/api/baseData/videoCamera";
import { trackTeam } from "@/api/workPlan/workPlan";
export default {
  name: "lamplist",
  data() {
    return {
      eleTabTop: 0, //表格容器eleTabTop距浏览器顶部距离
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 摄像机表格数据
      videoCameraList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: 2,
        name: null,
        ipaddress: null,
        occupyFlag: null,
      },
      // 表单参数
      form: { type: 2 },
      // 表单校验
      rules: {
        trackId: [{ required: true, message: "股道不能为空", trigger: "blur" }],
        complete: [
          { required: true, message: "列位不能为空", trigger: "blur" },
        ],
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        ipaddress: [
          { required: true, message: "智能灯编号不能为空", trigger: "blur" },
        ],
      },
      trackTeamLists: [],
      completeLists: [
        { complete: `1`, completeName: `1列位` },
        { complete: `2`, completeName: `2列位` },
      ],
    };
  },
  created() {
    this.getList();
    this.gettrackTeam();
  },
  mounted() {
    // 滚动条的获取
    this.handleScrollx();
    window.addEventListener("scroll", this.handleScrollx, true);
  },
  activated() {
    //获取表格容器距页面头部的距离
    this.handleScrollx();
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScrollx, true);
  },
  methods: {
    //获取表格容器距页面头部的距离
    handleScrollx() {
      this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top;
    },
    /** 查询摄像机列表 */
    getList() {
      this.loading = true;
      lamplist(this.queryParams).then((response) => {
        this.videoCameraList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        ipaddress: null,
        occupyFlag: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        type: 2,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "智能灯-新增";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getVideoCamera(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "智能灯-修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateVideoCamera(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVideoCamera(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除智能灯？")
        .then(function () {
          return delVideoCamera(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "videoCamera/videoCamera/export",
        {
          ...this.queryParams,
        },
        `videoCamera_${new Date().getTime()}.xlsx`
      );
    },
    //股道台位下拉框
    gettrackTeam() {
      trackTeam().then((res) => {
        this.trackTeamLists = res.rows;
      });
    },
  },
};
</script>
